package com.lc.jooq.project.configuration;

import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DefaultDSLContext;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by liangchao03 on 2017/6/28.
 */
@Configuration
public class DataSourceConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean
    public DSLContext dslContext(){
        Connection connection = null;
        try {
            connection = dataSource().getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DSLContext context = new DefaultDSLContext(connection, SQLDialect.MYSQL);
        return context;
    }

}
